package chabernac.beanutils.facade;

import java.beans.IntrospectionException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import chabernac.beanutils.CopyBean;

public class BeanUtils {
	private static Map<String, CopyBean> cachedCopyBeans = new ConcurrentHashMap<String, CopyBean>();

	public static void copyBean(Object aSource, Object aTarget) throws IntrospectionException {
		if (aSource == null)
			throw new IllegalArgumentException("Source can not be null");
		if (aTarget == null)
			throw new IllegalArgumentException("Target can not be null");
		String theId = aSource.getClass().getPackage() + aSource.getClass().getName() + aTarget.getClass().getPackage()
				+ aTarget.getClass().getName();
		if (!cachedCopyBeans.containsKey(theId)) {
			CopyBean theCopyBean = new CopyBean(aSource.getClass(), aTarget.getClass());
			cachedCopyBeans.put(theId, theCopyBean);
		}
		cachedCopyBeans.get(theId).copy(aSource, aTarget);
	}
}
